package sdu.icat.pj.sys.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import sdu.icat.pj.base.ResponseMap;
import sdu.icat.pj.util.InstanceUtil;
import sdu.icat.pj.util.UploadUtil;

@RestController
@Api(value="file upload",tags={"upload"})
@RequestMapping(value="/upload",method=RequestMethod.POST)
public class UploadController {

    /**
     * 上传文件(支持批量)
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/temp/file")
    @ApiOperation(value="上传文件")
    public ResponseMap uploadFile(HttpServletRequest request,HttpServletResponse response) {
	ResponseMap result = new ResponseMap();
	List<String> fileNames = UploadUtil.uploadFile(request);
	if(fileNames.size() > 0) {
	    return result.setAttribute("fileNames", fileNames).setSuccessMap("上传成功", null);
	} else {
	    return result.setFailedMap("请选择要上传的文件", null);
	}
    }

    /**
     * 上传文件(支持批量)
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/temp/image")
    @ApiOperation(value="上传图片")
    public ResponseMap uploadImage(HttpServletRequest request, HttpServletResponse response) {
	ResponseMap result = new ResponseMap();
	List<String> fileNames = UploadUtil.uploadImage(request, false);
	if (fileNames.size() > 0) {
	    return result.setAttribute("fileNames", fileNames).setSuccessMap("上传成功", null);
	} else {
	    return result.setFailedMap("请选择要上传的图片", null);
	}
    }


    /**
     * 上传文件(支持批量)
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/temp/imageData")
    @ApiOperation(value="上传图片")
    public ResponseMap uploadImageData(HttpServletRequest request, HttpServletResponse response) {
	ResponseMap result = new ResponseMap();
	List<String> fileNames = UploadUtil.uploadImageData(request);
	if (fileNames.size() > 0) {
	    return result.setAttribute("fileNames", fileNames).setSuccessMap("上传成功", null);
	} else {
	    return result.setFailedMap("请选择要上传的图片", null);
	}
    }

    /**
     * 上传图片
     * 
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/image")
    @ApiOperation(value = "上传图片")
    public ResponseMap uploadImage2Ftp(HttpServletRequest request, HttpServletResponse response) {
	ResponseMap result = new ResponseMap();
	List<String> fileNames = UploadUtil.uploadImage(request, false);
	if (fileNames.size() > 0) {
	    List<String> resultList = InstanceUtil.newArrayList();
	    for (int i = 0; i < fileNames.size(); i++) {
		String filePath = UploadUtil.getUploadDir(request) + fileNames.get(i);
		String file = UploadUtil.remove2ftp("image", filePath);
		resultList.add(file);
	    }
	    return result.setAttribute("fileNames", resultList).setSuccessMap("上传成功", null);
	} else {
	    return result.setFailedMap("请选择要上传的图片", null);
	}
    }
}
